package com.agu.mgt.example.controller;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.agu.common.annotation.SysLog;
import com.agu.common.excel.Column;
import com.agu.common.excel.ExcelExport;

import com.agu.utils.xss.XssHttpServletRequestWrapper;
import com.alibaba.fastjson.JSON;
import com.agu.mgt.sys.controller.AbstractController;
import com.agu.entity.example.ExmMessageEntity;
import com.agu.mgt.example.service.ExmMessageService;
import com.agu.utils.Query;
import com.github.pagehelper.Page;
import com.agu.result.Result;
import com.agu.result.ResultGenerator;
import com.agu.utils.CompareUtils;
import com.agu.utils.FormatUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 
 * @author 
 * @email 
 * @date 2019-01-10 16:46:24
 */
 @SuppressWarnings("rawtypes")
@RestController
@Api("")
@RequestMapping("exmmessage")
public class ExmMessageController extends AbstractController{
	@Autowired
	private ExmMessageService exmMessageService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("exmmessage:list")
	public Result list(@RequestBody Map params){

		// 查询列表数据
		Query query = new Query(params);
		Page<ExmMessageEntity> page = exmMessageService.queryPageList(query);
		return ResultGenerator.successPage(page);
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/info/{messageId}")
	@ApiOperation(value = "信息", notes = "说明")
	@RequiresPermissions("exmmessage:info")
	public Result info(@PathVariable("messageId") Long messageId){
		ExmMessageEntity exmMessage = exmMessageService.queryObject(messageId);
		
		return ResultGenerator.successData(exmMessage);
	}
	
	/**
	 * 保存
	 */
	@SysLog("新增")
	@PostMapping("/save")
	@ApiOperation(value = "保存", notes = "说明")
	@RequiresPermissions("exmmessage:save")
	public Result save(@RequestBody ExmMessageEntity exmMessage){
	
		exmMessageService.save(exmMessage);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("title","标题");
		     cmap.put("content","内容");
		     cmap.put("msgLevel","sp等级");
		     cmap.put("status","状态  ");
		String remark = CompareUtils.getObjectRemark(exmMessage, cmap);
		return ResultGenerator.success("保存成功").setRemark(remark);
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改")
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "说明")
	@RequiresPermissions("exmmessage:update")
	public Result update(@RequestBody ExmMessageEntity exmMessage){
	
		ExmMessageEntity exmMessage1 = exmMessageService.queryObject(exmMessage.getMessageId());
	
		exmMessageService.update(exmMessage);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		
		     cmap.put("title","标题");
		     cmap.put("content","内容");
		     cmap.put("msgLevel","sp等级");
		     cmap.put("status","状态  ");
	
		String remark = CompareUtils.getTwoObjectRemark(exmMessage1, exmMessage, cmap);
	
		return ResultGenerator.success("修改成功").setRemark(remark);
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除")
	@GetMapping("/delete/{messageId}")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("exmmessage:delete")
	public Result delete(@PathVariable("messageId") Long messageId){
	
	    	if (messageId == null) {
			return ResultGenerator.fail("缺少参数");
		}
	
		ExmMessageEntity exmMessage = exmMessageService.queryObject(messageId);
	
		exmMessageService.delete(messageId);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("title","标题");
		     cmap.put("content","内容");
		     cmap.put("msgLevel","sp等级");
		     cmap.put("status","状态  ");
		String remark = CompareUtils.getObjectRemark(exmMessage, cmap);
		return ResultGenerator.success("删除成功").setRemark(remark);
	}
	
	/**
	 * 批量删除
	 */
	@SysLog("批量删除")
	@PostMapping("/deletes")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("exmmessage:delete")
	public Result deletes(@RequestBody Long[] messageIds){
	
		exmMessageService.deleteBatch(messageIds);
		
		return ResultGenerator.success("删除成功");
	}
	
	@RequestMapping("/download")
	@ApiOperation(value = "下载", notes = "说明")
	@RequiresPermissions("exmmessage:list")
	public void download(HttpServletRequest request, HttpServletResponse response) {

		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
		
		String query = orgRequest.getParameter("query");

		HashMap querymap = JSON.parseObject(query, HashMap.class);

		List<ExmMessageEntity> list = exmMessageService.queryList(querymap);

		if (list != null && list.size() > 0) {
			Map map = new HashMap();

               Map<Integer, String> msgLevelMap = new HashMap<Integer, String>();

                msgLevelMap.put(1, "一级");
                msgLevelMap.put(2, "二级");
                msgLevelMap.put(3, "三级");
                msgLevelMap.put(4, "四级");
            map.put("msgLevelMap", msgLevelMap);
               Map<Integer, String> statusMap = new HashMap<Integer, String>();

                statusMap.put(0, "禁用");
                statusMap.put(1, "正常");
            map.put("statusMap", statusMap);

			List<Column> clist = new ArrayList();
			Column cloumn = null;

            cloumn = new Column("企业ID", "messageId", 1);
			clist.add(cloumn);
            cloumn = new Column("标题", "title", 1);
			clist.add(cloumn);
            cloumn = new Column("内容", "content", 1);
			clist.add(cloumn);
            cloumn = new Column("sp等级", "msgLevel", 2);
			clist.add(cloumn);
            cloumn = new Column("状态  ", "status", 2);
			clist.add(cloumn);
            cloumn = new Column("更新者ID", "updateUserId", 1);
			clist.add(cloumn);
            cloumn = new Column("更新时间", "updateTime", 1);
			clist.add(cloumn);
            cloumn = new Column("创建者ID", "createUserId", 1);
			clist.add(cloumn);
            cloumn = new Column("创建时间", "createTime", 1);
			clist.add(cloumn);
			try {
				ExcelExport export = exportList(list, clist, map);
				if (export != null) {
					this.downExcel(export, response);
					//this.downzip(export, response);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
